Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Xml

Namespace Helpers
	Public Class XMLProvider
		Public Sub GetConnectionInfo(ByRef ErrorText As String)
			ErrorText = ""

			Dim xmlDoc As XmlDocument = XMLReader(GlobalVars.g_strPathConfig, ErrorText)

			If ErrorText <> "" Then
				Return
			End If

			Dim xmlEle As XmlElement = xmlDoc.DocumentElement

			GlobalVars.g_strDataSource = xmlEle.SelectSingleNode("servname").InnerText
			GlobalVars.g_strDataBaseName = xmlEle.SelectSingleNode("database").InnerText
		End Sub

		Public Function XMLReader(filename As String, ByRef ErrorText As String) As XmlDocument
			Dim xmlR As New XmlDocument()
			Try
				xmlR.Load(filename)
			Catch
				ErrorText = "Không đọc được hoặc không tồn tại tập tin cấu hình"
			End Try

			Return xmlR
		End Function

		Public Sub XMLWriter(filename As String)
			Dim xmlW As New XmlTextWriter(filename, Nothing)
			xmlW.Formatting = Formatting.Indented

			xmlW.WriteStartDocument()
			xmlW.WriteComment(vbLf & "Khong duoc thay doi noi dung file nay!" & vbLf & "Thong so co ban:" & vbLf & vbTab & "costatus = true : quyen Windows" & vbLf & vbTab & "costatus = false: quyen SQL Server" & vbLf & vbTab & "servname: ten server" & vbLf & vbTab & "username: ten dang nhap he thong" & vbLf & vbTab & "password: mat khau dang nhap he thong" & vbLf & vbTab & "database: ten co so du lieu" & vbLf)
			xmlW.WriteStartElement("config")

			xmlW.WriteStartElement("costatus")
			xmlW.WriteEndElement()

			xmlW.WriteStartElement("servname")
			xmlW.WriteEndElement()

			xmlW.WriteStartElement("username")
			xmlW.WriteEndElement()

			xmlW.WriteStartElement("password")
			xmlW.WriteEndElement()

			xmlW.WriteStartElement("database")
			xmlW.WriteEndElement()

			xmlW.WriteEndElement()
			xmlW.WriteEndDocument()

			xmlW.Close()
		End Sub

		Public Sub XMLWriter(filename As [String], servname As [String], database As [String], costatus As [String])
			Dim xmlW As New XmlTextWriter(filename, Nothing)
			xmlW.Formatting = Formatting.Indented

			xmlW.WriteStartDocument()
			xmlW.WriteComment(vbLf & "Khong duoc thay doi noi dung file nay!" & vbLf & "Thong so co ban:" & vbLf & vbTab & "costatus = true : quyen Windows" & vbLf & vbTab & "costatus = false: quyen SQL Server" & vbLf & vbTab & "servname: ten server" & vbLf & vbTab & "username: ten dang nhap he thong" & vbLf & vbTab & "password: mat khau dang nhap he thong" & vbLf & vbTab & "database: ten co so du lieu" & vbLf)
			xmlW.WriteStartElement("config")

			xmlW.WriteStartElement("costatus")
			xmlW.WriteString(costatus)
			xmlW.WriteEndElement()

			xmlW.WriteStartElement("servname")
			xmlW.WriteString(servname)
			xmlW.WriteEndElement()

			xmlW.WriteStartElement("username")
			xmlW.WriteString("")
			xmlW.WriteEndElement()

			xmlW.WriteStartElement("password")
			xmlW.WriteString("")
			xmlW.WriteEndElement()

			xmlW.WriteStartElement("database")
			xmlW.WriteString(database)
			xmlW.WriteEndElement()

			xmlW.WriteEndElement()
			xmlW.WriteEndDocument()

			xmlW.Close()
		End Sub

		Public Sub XMLWriter(filename As [String], servname As [String], database As [String], username As [String], password As [String], costatus As [String])
			Dim xmlW As New XmlTextWriter(filename, Nothing)
			xmlW.Formatting = Formatting.Indented

			xmlW.WriteStartDocument()
			xmlW.WriteComment(vbLf & "Khong duoc thay doi noi dung file nay!" & vbLf & "Thong so co ban:" & vbLf & vbTab & "costatus = true : quyen Windows" & vbLf & vbTab & "costatus = false: quyen SQL Server" & vbLf & vbTab & "servname: ten server" & vbLf & vbTab & "username: ten dang nhap he thong" & vbLf & vbTab & "password: mat khau dang nhap he thong" & vbLf & vbTab & "database: ten co so du lieu" & vbLf)
			xmlW.WriteStartElement("config")

			xmlW.WriteStartElement("costatus")
			xmlW.WriteString(costatus)
			xmlW.WriteEndElement()

			xmlW.WriteStartElement("servname")
			xmlW.WriteString(servname)
			xmlW.WriteEndElement()

			xmlW.WriteStartElement("username")
			xmlW.WriteString(username)
			xmlW.WriteEndElement()

			xmlW.WriteStartElement("password")
			xmlW.WriteString(password)
			xmlW.WriteEndElement()

			xmlW.WriteStartElement("database")
			xmlW.WriteString(database)
			xmlW.WriteEndElement()

			xmlW.WriteEndElement()
			xmlW.WriteEndDocument()

			xmlW.Close()
		End Sub
	End Class
End Namespace
